package cfs.model;

import org.mybeans.dao.DAOException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;

import cfs.databean.Fund;

public class FundDAO extends GenericDAO<Fund> {
	public FundDAO() {
		super(Fund.class, "fund", "fund_id");
		this.setUseAutoIncrementOnCreate(true);
		this.setIdleConnectionCleanup(true);
	}

	public Fund lookup(String symbol) throws DAOException {
		try {
			Fund[] funds = getFactory()
					.match(MatchArg.equals("symbol", symbol));
			if (funds.length <= 0) {
				return null;
			} else {
				return funds[0];
			}

		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public synchronized Fund[] getFunds() throws DAOException {
		try {
			Fund[] funds = this.getFactory().match();
			return funds;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public synchronized Fund getFundMap(int fund_id) throws DAOException {
		try {
			Fund[] funds = this.getFactory().match(
					MatchArg.equals("fund_id", fund_id));
			if (funds.length > 0) {
				return funds[0];
			} else {
				return null;
			}

		} catch (RollbackException e) {
			e.printStackTrace();
			throw new DAOException(e);
		}
	}

	public boolean isFundExist(String sym) throws DAOException {

		try {
			Fund[] fund;
			fund = this.getFactory().match(MatchArg.equals("symbol", sym));
			return fund.length > 0;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
}
